#!coding=utf-8
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import *
from django.views.decorators.csrf import csrf_exempt
from util.form_util import *

from enterprise_chemicals.models import *
from enterprise_basic_information.models import *
from park.models import *


# 获取当前园区 和 企业列表
def isLogin(req):
	admin = req.session.get('admin', None)
	park = req.session.get('park', None)
	
	if not admin and not park:
		return False, False
	else:
		es = Enterprise.objects.filter(park = park)

		if len(es) > 0:
			return park, es
		else:
			return False, False

def productList(request):
	p, es = isLogin(request)

	if not p and not es:
		return HttpResponse('当前用户信息错误')

	year = request.GET.get('y', None)
	month = request.GET.get('m', None)
	eid = request.GET.get('eid', None)
	ename = request.GET.get('ename', None)

	try:
		ps = []
		pds = Product.objects.all()

		if year and month:
			pds = pds.filter(add_time__year=int(year), add_time__month=int(month))

		if eid or ename:
			if eid:
				pds = pds.filter(enterprise = int(eid))
				
			if ename:
				pds = pds.filter(enterprise__name__contains = ename)

			ps.extend(pds)
		else:
			for e in es:
				ps.extend(pds.filter(enterprise=e))

		paginator = Paginator(ps, 10)
		page = request.GET.get('page')
		ps = paginator.page(page)
	except PageNotAnInteger:
		ps = paginator.page(1)
	except EmptyPage:
		ps = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)
 	
	return render_to_response('park/chemical/product/product_list.html', {  'ps':ps, 'es':es, 'paginator':paginator })

def productDetail(request, id):
	p = Product.objects.get(id = id)
	iis = Ingredients.objects.filter(product=p)
	return render_to_response('park/chemical/product/product_detail.html', { 'p':p, 'iis':iis }, context_instance=RequestContext(request))

def materialList(request):
	p, es = isLogin(request)

	if not p and not es:
		return HttpResponse('当前用户信息错误')

	year = request.GET.get('y', None)
	month = request.GET.get('m', None)
	eid = request.GET.get('eid', None)
	ename = request.GET.get('ename', None)

	try:
		ms = []
		mas = Materials.objects.all()

		if year and month:
			mas = mas.filter(add_time__year=int(year), add_time__month=int(month))

		if eid or ename:
			if eid:
				mas = mas.filter(enterprise = int(eid))
				
			if ename:
				mas = mas.filter(enterprise__name__contains = ename)

			ms.extend(mas)
		else:
			for e in es:
				ms.extend(mas.filter(enterprise=e))

		paginator = Paginator(ms, 10)
		page = request.GET.get('page')
		ms = paginator.page(page)
	except PageNotAnInteger:
		ms = paginator.page(1)
	except EmptyPage:
		ms = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)
 	
	return render_to_response('park/chemical/materials/materials_list.html', {  'ms':ms, 'es':es, 'paginator':paginator })

def materialDetail(request, id):
	m = Materials.objects.get(id = id)
	return render_to_response('park/chemical/materials/materials_detail.html', { 'm' : m }, context_instance=RequestContext(request))

def chemicalsList(request):
	p, es = isLogin(request)
	if not p and not es:
		return HttpResponse('当前用户信息错误')

	year = request.GET.get('y', None)
	month = request.GET.get('m', None)
	eid = request.GET.get('eid', None)
	ename = request.GET.get('ename', None)

	try:
		cs = []
		ccs = Chemicals.objects.all()

		if year and month:
			ccs = ccs.filter(add_time__year=int(year), add_time__month=int(month))

		if eid or ename:
			if eid:
				ccs = ccs.filter(enterprise = int(eid))
				
			if ename:
				ccs = ccs.filter(enterprise__name__contains = ename)

			cs.extend(ccs)
		else:
			for e in es:
				cs.extend(ccs.filter(enterprise=e))

		paginator = Paginator(cs, 10)
		page = request.GET.get('page')
		cs = paginator.page(page)
	except PageNotAnInteger:
		cs = paginator.page(1)
	except EmptyPage:
		cs = paginator.page(paginator.num_pages)
	except Exception as e:
		return HttpResponse("非法请求\n\n"+ e.message)
 	
	return render_to_response('park/chemical/chemicals/chemicals_list.html', {  'cs':cs, 'es':es, 'paginator':paginator })

def chemicalsDetail(request,id):
	c = Chemicals.objects.get(id = id)
	return render_to_response('park/chemical/chemicals/chemicals_detail.html', { 'c' : c}, context_instance=RequestContext(request))

